import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/use_request/new_choose_goods_view_model.dart';

import '../../net/api/http_api.dart';
import '../../util/image_utils.dart';

class NewChooseGoodsPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _NewChooseGoodsState();
}

class _NewChooseGoodsState
    extends BaseMvvmListState<NewChooseGoodsViewModel, NewChooseGoodsPage> {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text("选择物资"),
      ),
      body: Column(
        children: [
          Row(
            children: [
              CommonInput(
                isDense: true,
                padding: EdgeInsets.zero,
                border: InputBorder.none,
                hintText: '输入物资编号/物资名称，进行搜索',
                textInputAction: TextInputAction.search,
                onChanged: (str){
                  viewModel.searchWords = str;
                },
                onSubmitted: (str){
                  viewModel.requestRefresh();
                },
              ).setWeight(1),
              Icon(Icons.search).onTap(() {
                viewModel.requestRefresh();
              })
            ],
          ).addToContainer(
            padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10),
            margin: EdgeInsets.symmetric(horizontal: 16,vertical: 12),
              decoration: BoxDecoration(
                  color: Colours.bg_color, borderRadius: 20.borderRadius)),
          // Row(
          //   children: [
          //     SizedBox(width: 16,),
          //     Column(
          //       crossAxisAlignment: CrossAxisAlignment.start,
          //       children: [
          //         CommonText('是否为固定资产',textSize: 15),
          //         SizedBox(height: 8,),
          //         Row(
          //           children: [
          //             CommonText('${vm?.selectFixedAssets?.name??"全部"}').setWeight(1),
          //             Icon(Icons.arrow_drop_down)
          //           ],
          //         ).addToContainer(
          //           decoration: BoxDecoration(
          //             color: Colours.bg_color,
          //             borderRadius: 4.borderRadius
          //           ),
          //           padding: EdgeInsets.symmetric(horizontal: 8,vertical: 4)
          //         ).onTap(() {
          //           viewModel.selectFix();
          //         })
          //       ],
          //     ).setWeight(1),
          //     VerticalDivider(),
          //     Column(
          //       crossAxisAlignment: CrossAxisAlignment.start,
          //       children: [
          //         CommonText('是否捐赠',textSize: 15),
          //         SizedBox(height: 8,),
          //         Row(
          //           children: [
          //             CommonText('${vm?.selectDonate?.name??"全部"}').setWeight(1),
          //             Icon(Icons.arrow_drop_down)
          //           ],
          //         ).addToContainer(
          //             decoration: BoxDecoration(
          //                 color: Colours.bg_color,
          //                 borderRadius: 4.borderRadius
          //             ),
          //             padding: EdgeInsets.symmetric(horizontal: 8,vertical: 4)
          //         ).onTap(() {
          //           viewModel.selectDo();
          //         })
          //       ],
          //     ).setWeight(1),
          //     SizedBox(width: 16,),
          //   ],
          // ),

          loadingContentWidget.setWeight(1)
        ],
      ),
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    var image = "${HttpApi.imageHead}${item.attatchId}";
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Stack(
          children: [
            ImageLoad(
              item.attatchId == null ? ImageUtils.getImgPath("none") : image,
              isAsset: item.attatchId == null,
              errorImage: ImageUtils.getImgPath("none"),
              width: 80,
              height: 80,
              fit: BoxFit.cover,
            ).toRound(borderRadius: 10.borderRadius),
            CommonText(item.materialTypeName ?? "",
                    textSize: 10,
                    fontWeight: FontWeight.bold,
                    textColor: Colors.white)
                .addToContainer(
                    padding: EdgeInsets.symmetric(horizontal: 4, vertical: 3),
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                            colors: [Colors.blue, Colors.lightBlueAccent],
                            begin: Alignment.topLeft),
                        borderRadius: 4.borderRadius))
                .setLocation(right: 3, top: 4)
          ],
        ).addToContainer(width: 80, height: 80),
        Container(
          width: 8,
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: 5,
            ),
            CommonText('物资编号：${item.materialNo??""}',textSize: 14),
            RichText(
                text: TextSpan(
                    text: "${item.materialName}",
                    style: TextStyle(color: Colors.black, fontSize: 14),
                    children: [
                  if (!item.model.isNullOrEmpty())
                    TextSpan(
                        text: "  型号:${item.model}",
                        style: TextStyle(fontSize: 12, color: Colors.grey))
                ])),

            // Container(height: 5,),
            // CommonText(item.branchName.isNullOrEmpty()?"":"维护机构：${item.branchName}",textSize: 12,textColor: Colors.grey[700]),
            Container(
              height: 5,
            ),
            CommonText(item.brand.isNullOrEmpty() ? "" : "品牌：${item.brand}",
                textSize: 12, textColor: Colors.grey[700]!),
            Container(
              height: 5,
            ),
            Row(
              children: [
                CommonText(
                  "库存：${item.inventoryQuantity.d}${item.unit ?? ""}",
                  textSize: 12,
                ),
                if((item.applyQuantity??0)>0)
                  ...[
                    SizedBox(width: 18,),
                    CommonText(
                      "申请中：${item.applyQuantity.d}",
                      textColor: Colors.red,
                      textSize: 12,
                    ),
                  ],
                SizedBox().setWeight(1),
                TextButton(
                  onPressed: () {
                    if ((item.inventoryQuantity??0) <= 0) {
                      showToast("库存不足");
                      return;



                    }
                    finish(result: item);
                  },
                  child:
                      CommonText("添加", textSize: 12, textColor: Colors.white),
                  style: ButtonStyle(
                    shape: WidgetStateProperty.all(StadiumBorder()),
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                  ),
                ).addToContainer(height: 30)
              ],
            )
          ],
        ).setWeight(1)
      ],
    );
  }

  @override
  bool get canPullUp => true;

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.symmetric(horizontal: 16, vertical: 12);

  @override
  Widget get separatorDivider => Divider();

  @override
  NewChooseGoodsViewModel createViewModel() {
    return NewChooseGoodsViewModel(context);
  }
}
